package icst.cyber.hrm.bo;

import icst.cyber.hrm.dto.AccountDTO;
import icst.cyber.hrm.dto.EmpWorkDTO;
import icst.cyber.hrm.forms.LogonSessionForm;
import icst.cyber.hrm.mapper.PersonMapperDB;

import org.apache.log4j.Logger;

/**
 * @author NGOCLAN
 * 
 */
public class PersonBO {
	protected Logger log = Logger.getLogger(PersonBO.class);

	public PersonBO() {
		super();
	}

	/**
	 * Check valid account
	 * 
	 * @param username
	 * @param password
	 * @return boolean
	 * @throws Exception
	 */
	public boolean isValidAccount(String username, String password)
			throws Exception {
		log.debug("PersonBO.isValidAccount.start");

		boolean flag = false;
		PersonMapperDB mapper = null;
		AccountDTO accDTO = null;

		try {
			mapper = new PersonMapperDB();
			String md5Pass = CommonBO.encryptMD5(password);
			accDTO = mapper.getOneAccount(username, md5Pass);
			if (accDTO != null) {
				flag = true;
			}
		} catch (Exception e) {
			log.error("PersonBO.isValidAccount", e);
		} finally {
			mapper.closeConnection();
		}

		log.debug("PersonBO.isValidAccount.end");
		return flag;
	}

	/**
	 * Get id_emp
	 * 
	 * @param username
	 * @param password
	 * @return id_emp
	 * @throws Exception
	 */
	public int getIdEmpByAccount(String username, String password)
			throws Exception {
		log.debug("PersonBO.getIdEmpByAccount.start");

		int id_emp = 0;
		PersonMapperDB mapper = null;
		AccountDTO accDTO = null;

		try {
			mapper = new PersonMapperDB();
			String md5Pass = CommonBO.encryptMD5(password);
			accDTO = mapper.getOneAccount(username, md5Pass);
			if (accDTO != null) {
				id_emp = accDTO.getId_emp();
			}
		} catch (Exception e) {
			log.error("PersonBO.getIdEmpByAccount", e);
		} finally {
			mapper.closeConnection();
		}

		log.debug("PersonBO.getIdEmpByAccount.end");
		return id_emp;
	}

	/**
	 * Check active employee (not block)
	 * 
	 * @param id_emp
	 * @return boolean
	 * @throws Exception
	 */
	public boolean isActiveEmployee(int id_emp) throws Exception {
		log.debug("PersonBO.isActiveEmployee.start");

		boolean flag = false;
		PersonMapperDB mapper = null;
		EmpWorkDTO workDTO = null;

		try {
			mapper = new PersonMapperDB();
			workDTO = mapper.getOneEmpWork(id_emp);
			if (workDTO != null && workDTO.getStatus() == true) {
				flag = true;
			}
		} catch (Exception e) {
			log.error("PersonBO.isActiveEmployee", e);
		} finally {
			mapper.closeConnection();
		}

		log.debug("PersonBO.isActiveEmployee.end");
		return flag;
	}

	public LogonSessionForm getSessionLogon(int id_emp) throws Exception {
		log.debug("PersonBO.getSessionLogon.start");

		PersonMapperDB mapper = null;
		AccountDTO accDTO = null;
		EmpWorkDTO workDTO = null;
		LogonSessionForm sessionForm = new LogonSessionForm();

		try {
			mapper = new PersonMapperDB();
			accDTO = mapper.getOneAccount(id_emp);
			workDTO = mapper.getOneEmpWork(id_emp);
			if (accDTO != null && workDTO != null) {
				sessionForm.setId_emp(accDTO.getId_emp());
				sessionForm.setUsername(accDTO.getUsername());
				sessionForm.setId_role(workDTO.getId_role());
			}
		} catch (Exception e) {
			log.error("PersonBO.getSessionLogon", e);
		} finally {
			mapper.closeConnection();
		}

		log.debug("PersonBO.getSessionLogon.end");
		return sessionForm;
	}
}
